package com.lewis.module.m2.api.bill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lewis.common.core.entity.page.PageResult;
import com.lewis.common.core.utils.StringUtil;
import com.lewis.module.m2.api.bill.entity.Bill;
import com.lewis.module.m2.api.bill.mapper.BillMapper;
import com.lewis.module.m2.api.bill.service.IBillService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * billService业务层处理
 *
 * @author lewis
 * @date 2021-11-02
 */
@Service
public class BillServiceImpl extends ServiceImpl<BillMapper, Bill> implements IBillService {

	@Resource
	private BillMapper billMapper;

	/**
	 * 查询bill
	 *
	 * @param id bill主键
	 * @return bill
	 */
	@Override
	public Bill selectBillById(Long id) {
		return billMapper.selectById(id);
	}

	/**
	 * 查询bill列表
	 *
	 * @param bill bill
	 * @return bill
	 */
	@Override
	public List<Bill> selectBillList(Bill bill) {
		LambdaQueryWrapper<Bill> lqw = new LambdaQueryWrapper<>();
		lqw.eq(StringUtil.isNotNull(bill.getId()), Bill::getId, bill.getId());
		lqw.eq(StringUtil.isNotNull(bill.getBillCodeId()), Bill::getBillCodeId, bill.getBillCodeId());
		lqw.eq(StringUtil.isNotNull(bill.getBillType()), Bill::getBillType, bill.getBillType());
		lqw.eq(StringUtil.isNotNull(bill.getUserId()), Bill::getUserId, bill.getUserId());
		lqw.like(StringUtil.isNotNull(bill.getRemark()), Bill::getRemark, bill.getRemark());
		lqw.like(StringUtil.isNotNull(bill.getCtime()), Bill::getCtime, bill.getCtime());
		lqw.like(StringUtil.isNotNull(bill.getUtime()), Bill::getUtime, bill.getUtime());
		return billMapper.selectList(lqw);
	}

	/**
	 * 查询bill列表 - 分页
	 *
	 * @param bill bill
	 * @return bill
	 */
	@Override
	public PageResult getBillList(Bill bill) {
		LambdaQueryWrapper<Bill> lqw = new LambdaQueryWrapper<>();
		lqw.eq(StringUtil.isNotNull(bill.getId()), Bill::getId, bill.getId());
		lqw.eq(StringUtil.isNotNull(bill.getBillCodeId()), Bill::getBillCodeId, bill.getBillCodeId());
		lqw.eq(StringUtil.isNotNull(bill.getBillType()), Bill::getBillType, bill.getBillType());
		lqw.eq(StringUtil.isNotNull(bill.getUserId()), Bill::getUserId, bill.getUserId());
		lqw.like(StringUtil.isNotNullAndNotBlank(bill.getRemark()), Bill::getRemark, bill.getRemark());
		lqw.like(StringUtil.isNotNull(bill.getCtime()), Bill::getCtime, bill.getCtime());
		lqw.like(StringUtil.isNotNull(bill.getUtime()), Bill::getUtime, bill.getUtime());
		Page<Bill> billPage = billMapper.selectPage(new Page<>(bill.getPageIndex(), bill.getPageSize()), lqw);
		return PageResult.ok().setData(billPage.getRecords()).setTotal(billPage.getTotal());
	}

	/**
	 * 新增bill
	 *
	 * @param bill bill
	 * @return 结果
	 */
	@Override
	public int insertBill(Bill bill) {
		return billMapper.insert(bill);
	}

	/**
	 * 修改bill
	 *
	 * @param bill bill
	 * @return 结果
	 */
	@Override
	public int updateBill(Bill bill) {
		return billMapper.updateById(bill);
	}

	/**
	 * 批量删除bill
	 *
	 * @param ids 需要删除的bill主键
	 * @return 结果
	 */
	@Override
	public int deleteBillByIds(Long[] ids) {
		List<Long> idList = new ArrayList<>(Arrays.asList(ids));
		return billMapper.deleteBatchIds(idList);
	}

	/**
	 * 删除bill信息
	 *
	 * @param id bill主键
	 * @return 结果
	 */
	@Override
	public int deleteBillById(Long id) {
		return billMapper.deleteById(id);
	}
}